<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
  <style>
    .el-page-header__content{
      color: white;
    }
  </style>
</head>
<body>
<div id="app">
  <el-page-header style="background-color: #0096dc;
  color: white;line-height: 60px"
                  @back="goBack" content="添加轮播图页面">
  </el-page-header>
  <el-form label-width="100px">
    <el-form-item label="轮播图图片">
      <!--上传组件开始-->
      <el-upload
              name="imageFile"
              action="/upload/images"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <!--上传组件结束-->
    </el-form-item>
    <el-form-item label="显示顺序">
      <el-input-number v-model="sort" :min="1" :max="50"></el-input-number>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="addBanner">上传轮播图</el-button>
    </el-form-item>
  </el-form>

</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!-- 引入AJAX -->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
 let v = new Vue({
    el: '#app',
    data: function() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        sort: 1,
        url: ""
      }
    },
   methods:{
     goBack() {
       console.log('go back');
       //返回上一页面
       history.back();
     },
     handleRemove(file, fileList) {
       console.log(file, fileList);
       axios.delete("/upload/images", {data: file.response}).then(res => {
          console.log(res);
       }).catch(err => {
          console.log(err);
       })
     },
     handlePictureCardPreview(file) {
       this.dialogImageUrl = file.url;
       this.dialogVisible = true;
     },
     handleSuccess(res, file) {
       console.log(res, file);
       this.url = res;
     },
     addBanner() {
       var banner = {
         sort: this.sort,
         url: this.url
       }
       console.log(banner);
       axios.post("/banners", banner).then(function (res) {
         console.log(res);
         // 返回上一页(admin.html)
         sessionStorage.setItem("activeIndex", "2-1");
         location.href = "admin.html"
       }).catch(function (err) {
         console.log(err);
         alert("添加失败");
       });
     }
   }
  })
</script>
</html>